/**
 * com.dingjian.basedata.org.service.impl.OrgServiceImpl.java
 */
package com.dingjian.basedata.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingjian.base.dao.BaseDao;
import com.dingjian.base.util.StringUtils;
import com.dingjian.basedata.org.dao.OrgDao;
import com.dingjian.basedata.org.enums.OrgStatusEnum;
import com.dingjian.basedata.org.model.Org;
import com.dingjian.basedata.org.service.OrgService;
import com.dingjian.basedata.service.impl.DataEntityService;

/**
 * @author FengMy
 * 
 * @since 2012-9-7
 */
@Component(value="orgService")
@Transactional
public class OrgServiceImpl extends DataEntityService<Org> implements OrgService {
	@Autowired
	private OrgDao orgDao;
	@Override
	protected BaseDao getDao() {
		return orgDao;
	}
	
	@Override
	public void addEntity(Org entity) {
		entity.setLongNumber(entity.getNumber());
		entity.setDisplayName(entity.getName());
		entity.setLevel(1);
		entity.setLeaf(true);
		entity.setStatus(OrgStatusEnum.CREATE);
		if(entity.getParent()!=null && !StringUtils.isEmpty(entity.getParent().getId())){
			Org parent = getEntityById(entity.getParent().getId());
			entity.setLongNumber(parent.getLongNumber() + "!" + entity.getNumber());
			entity.setDisplayName(parent.getDisplayName() + "_" + entity.getName());
			entity.setLevel(parent.getLevel() + 1);
			parent.setLeaf(false);
			super.updateEntity(parent);
		}
		super.addEntity(entity);
	}
	
	@Override
	public void updateEntity(Org entity) {
		entity.setLongNumber(entity.getNumber());
		entity.setDisplayName(entity.getName());
		entity.setLevel(1);
		entity.setLeaf(true);
		if(entity.getParent()!=null){
			Org parent = getEntityById(entity.getParent().getId());
			entity.setLongNumber(parent.getLongNumber() + "!" + entity.getNumber());
			entity.setDisplayName(parent.getDisplayName() + "_" + entity.getName());
			entity.setLevel(parent.getLevel() + 1);
			parent.setLeaf(false);
			super.updateEntity(parent);
		}
		super.updateEntity(entity);
	}
}
